package utils;

import org.apache.commons.lang.StringUtils;

import java.text.NumberFormat;
import java.text.ParsePosition;


public class StringUtil {

    public static boolean isNullOrEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static boolean isNumber(char chr) {
        return chr > '0' && chr < '9';
    }

    public static boolean isLetter(char chr) {
        return chr > 'A' && chr < 'Z' || chr > 'a' && chr < 'z';
    }

    public static boolean isNumeric(String str) {
        if (isNullOrEmpty(str)) {
            return false;
        }
        NumberFormat formatter = NumberFormat.getInstance();
        ParsePosition pos = new ParsePosition(0);
        formatter.parse(str, pos);
        return str.length() == pos.getIndex();
    }


    /**
     * byte数组转换成十六进制字符串
     *
     * @param src
     *            byte[]
     * @return 十六进制字符串
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder(32);
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString().toLowerCase();
    }

    /**
     * 十六进制字符串转换为byte数组
     *
     * @param hexString
     *            十六进制字符串
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }


    /**
     * char转换成byte
     *
     * @param c
     * @return
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    public static boolean isEmpty(String str){
        if(str == null || str.length()==0){
            return true;
        }
        return false;
    }

    /**
     * 如果任意字符串为空或者null
     * @param str
     * @return
     */
    public static boolean isAnyEmpty(String... str){
        for(String s: str){
            if(StringUtils.isEmpty(s)){
                return true;
            }
        }
        return false;
    }

    /**
     * 取f的n位小数
     * @param f
     * @param n
     */
    public static String getString(float f, int n){
        try {
            double m = Math.pow(10,n);
            double fd = Math.round(f * m) / m; //保留2位小数（）
            String ret = fd + "";
            if (ret.indexOf(".") > -1 && ret.length() > ret.indexOf(".") + n + 1) {
                ret = ret.substring(0, ret.indexOf(".") + n + 1);
            }
            return ret;
        }catch (Exception e){
            e.printStackTrace();
            return f+"";
        }
    }

    //转换对象to Long
    public static  Long getLong(Object obj){
        return  Long.valueOf(String.valueOf(obj));
    }
}
